
class Map
  constructor : () ->
    defaultTS = new Tileset("default")
    @sizeX = 32
    @sizeY = 32
    @layer0 = ((new Tile(defaultTS,1,0,x==@sizeX or y==@sizeY or x==0 or y==0) for y in [0..@sizeY]) for x in [0..@sizeX])
  toJSON: () ->
    return {
      type:"Map",
      sizeX:@sizeX,
      sizeY:@sizeY,
      layer0:((@layer0[x][y].toJSON() for y in [0..@sizeY]) for x in [0..@sizeX])
    }
  loadLayer: (layer) ->
      @layer0 = ((new Tile().load(layer[x][y]) for y in [0..@sizeY]) for x in [0..@sizeX])

class MapDisplayer
  constructor : (@screen) ->
    @map = new Map()
    @alphaTrick = on
    @pixeloffX = @pixeloffY = 0
  draw: () ->
    @screen.fillStyle = "#FFFFFF"
    @screen.fillRect(0,0,screenCanvas.width,screenCanvas.height)
    for y in [0..@map.sizeY]
      for x in [0..@map.sizeX] by 2
        if @alphaTrick is on then @checkAlphaTrick(x,y)
        @map.layer0[x][y].draw(@screen,x,y,@pixeloffX,@pixeloffY)
      for x in [1..@map.sizeX] by 2
        if @alphaTrick is on then @checkAlphaTrick(x,y)
        @map.layer0[x][y].draw(@screen,x,y,@pixeloffX,@pixeloffY)
  drawBlocks: () ->
    bt = new Tile("default",2,0)
    for y in [0..@map.sizeY]
      for x in [0..@map.sizeX] by 2
        if @alphaTrick is on then @checkAlphaTrick(x,y)
        if @map.layer0[x][y].blocks
           bt.draw(@screen,x,y,@pixeloffX,@pixeloffY)
      for x in [1..@map.sizeX] by 2
        if @alphaTrick is on then @checkAlphaTrick(x,y)
        if @map.layer0[x][y].blocks
           bt.draw(@screen,x,y,@pixeloffX,@pixeloffY)
  checkAlphaTrick: (x,y) ->
    if x== mouseFollower.gameX and y==mouseFollower.gameY
      @screen.globalAlpha = 0.5
    else
      @screen.globalAlpha = 1
  load: (map) ->
    @map.sizeX = map.sizeX
    @map.sizeY = map.sizeY
    @map.loadLayer(map.layer0)
